package main

import (
	"cyrus/go-from-basic-to-advanced-180/learn-go/03-interface/mock"
	"cyrus/go-from-basic-to-advanced-180/learn-go/03-interface/real"
	"fmt"
	"time"
)

type Retriever interface {
	Get(url string) (string, error)
}

func download(r Retriever) (string, error) {
	return r.Get("https://httpbin.org/get")
}

func main() {
	var r Retriever
	r = &mock.Retriever{Content: "Hello, 世界"}
	fmt.Printf("type of r: %T, value of r: %#v\n", r, r)

	r = &real.Retriever{Timeout: 5 * time.Second}
	fmt.Printf("type of r: %T, value of r: %#v\n", r, r)

	fmt.Println(download(r))

	// 断言1
	// switch r.(type) {
	// case *mock.Retriever:
	// 	fmt.Printf("【断言结果】mock retriever: %#v\n", r)
	// case *real.Retriever:
	// 	fmt.Printf("【断言结果】real retriever: %#v\n", r)
	// }
	switch v := r.(type) {
	case *mock.Retriever:
		fmt.Printf("【断言结果1】mock retriever: %#v\n", v.Content)
	case *real.Retriever:
		fmt.Printf("【断言结果1】real retriever: %#v\n", v.Timeout)
	}

	// 断言2
	if v, ok := r.(*real.Retriever); ok {
		fmt.Printf("【断言结果2】real retriever: %#v\n", v.Timeout)
	}

}
